/***************************************************************************
 *   Copyright (C) 2007 by Gaetano Mendola   *
 *   mendola@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef DICE_H
#define DICE_H

#include <stdlib.h>
#include <stdexcept>

namespace Support {

/**
    @author Gaetano Mendola <mendola@gmail.com>
*/
class Dice {
public:

    static Dice& instance();

    unsigned int value(double aMin = 0, double aMax = 100) const {
        return static_cast<unsigned int>(rand() * double(aMax-aMin) / RAND_MAX + aMin);
    }

    bool proc(double aValue) const {
        if ( aValue > 100.0 ) {
            throw std::runtime_error("The proc percentage can not be > 100.0");
        } else if ( aValue == 0.0 ) {
            return false;
        }

        return value(0.0, 100.0) <= aValue;
    }

private:
    Dice();
    ~Dice();

    //Disabled methods
    Dice(const Dice&);
    Dice& operator=(const Dice&);

}; //class Dice

} //namespace Support

#endif
